<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/CustomerCom.php");
require_once(dirname(__FILE__) . "/../../common/Common.php");
require_once(dirname(__FILE__) . "/../../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/C_serviceCom.php");

$usercom = new UserCom();
$customercom = new CustomerCom();
$common = new Common();
$ordercom = new OrderCom();
$cservicecom = new CservicetCom();

$userid = $common->getLogin();
if (!isset($userid) || empty($userid)) {
    header('Location:./../login.php');
}

$customerid = $_REQUEST['cid'];

//获取分页
if (empty($_REQUEST['p']) || !isset($_REQUEST['p'])) {
    $page = 1;
    $minlimit = 0;
} else {
    $page = $_REQUEST['p'];
    if ($page > 1) {
        $minlimit = 50 * $page;
    } else {
        $minlimit = 0;
    }
}
$limitnum = 50;

$cservicelist = $cservicecom->getAllCservice($customerid, $minlimit, $limitnum);
$number = $cservicecom->getNumber($customerid);

$p = (int) ($number / 50 + 1);

$res = array();
if (!empty($cservicelist)) {
    foreach ($cservicelist as $k => $v) {
        $res[$k]['id'] = $v['id'];
        $customer = $customercom->getCustomerById($v['customer_id']);
        $res[$k]["customer"] = $customer['nickname'];
        $service = $servicecom->getServiceById($v['service_id']);
        $res[$k]["service"] = $service["name"];
        $res[$k]['tradeno'] = $v['trade_no'];
        $res[$k]['number'] = $v['number'];
    }
}
?>
<!DOCTYPE html>
<html>

    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="keywords" content="">
        <meta name="description" content="">
        <link href="../public/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
        <link href="../public/css/font-awesome.css?v=4.4.0" rel="stylesheet">

        <link href="../public/css/animate.css" rel="stylesheet">
        <link href="../public/css/style.css?v=4.1.0" rel="stylesheet">

        <link href="../public/css/paging.css" rel="stylesheet">
        <link href="../public/css/showproject.css" rel="stylesheet">
    </head>

    <body class="gray-bg">

        <div class="wrapper wrapper-content animated fadeInUp">
            <div class="row">
                <div class="col-sm-12">

                    <div class="ibox">
                        <div class="ibox-title">
                            <h5>用户记次卡管理</h5>
                            <div class="ibox-tools">
                                <a href="javascript:history.go(-1);" class="btn btn-primary btn-xs">返回</a>
                            </div>
                        </div>
                        <div class="ibox-content">
                            <div class="row m-b-sm m-t-sm">
                                <div class="col-md-1">

                                </div>
                                <div class="col-md-11">
                                    <div class="input-group">

                                    </div>
                                </div>
                            </div>

                            <div class="project-list">

                                <div class="project-list">
                                    <div class="table-responsive">
                                        <table class="table table-striped">
                                            <thead>
                                                <tr>
                                                    <td class="project-title">
                                                        <span>服务</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>用户</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>剩余次数</span>
                                                    </td>
                                                    <td class="project-actions">
                                                        <span>操作</span>
                                                    </td>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                foreach ($res as $v) {
                                                    ?>
                                                    <tr id="list<?= $v['id'] ?>" data-id="<?= $v['id'] ?>">
                                                        <td class="project-title">
                                                            <span><?= $v['service'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['customer'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['number'] ?></span>
                                                        </td>
                                                        <td class="project-actions">
                                                            
                                                        </td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                        <div class="box" id="box"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        

        <!-- 全局js -->
        <script src="../public/js/jquery.min.js?v=2.1.4"></script>
        <script src="../public/js/bootstrap.min.js?v=3.3.6"></script>
        <script src="../public/js/plugins/layer/layer.min.js"></script>

        <!-- 自定义js -->
        <script src="../public/js/paging.js?v=1.0.0"></script>
        <script src="../public/js/upload.js?v=1.0.0"></script>
        <script src="../public/js/common.js?v=1.0.4"></script>

        <script>
            $(document).ready(function () {

                $('#loading-example-btn').click(function () {
                    btn = $(this);
                    simpleLoad(btn, true)

                    // Ajax example
                    //                $.ajax().always(function () {
                    //                    simpleLoad($(this), false)
                    //                });

                    simpleLoad(btn, false)
                });
            });

            function simpleLoad(btn, state) {
                if (state) {
                    btn.children().addClass('fa-spin');
                    btn.contents().last().replaceWith(" Loading");
                } else {
                    setTimeout(function () {
                        btn.children().removeClass('fa-spin');
                        btn.contents().last().replaceWith(" Refresh");
                    }, 2000);
                }
            }
            var setTotalCount = <?= $number ?>;
            $('#box').paging({
                initPageNo: <?= $page ?>, // 初始页码
                totalPages: <?= $p ?>, //总页数
                totalCount: '合计' + setTotalCount + '条数据', // 条目总数
                slideSpeed: 600, // 缓动速度。单位毫秒
                jump: true, //是否支持跳转
                callback: function (page) { // 回调函数
                    console.log(page);
                }
            });

            $(".btn-det").click(function () {
                var orderid = $(this).data("id");
                var datastr = "cont=1&orderid=" + orderid;
                $.ajax({
                    url: "/../../controller/order.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            layer.msg('删除成功', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            $("#list" + result.data).css("display", "none");
                        } else {
                            layer.msg('删除失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        return;
                    }
                });
            });
            //初始化订单详情弹窗
            function cleanOrderdetail() {
                $("#customer").val("");
                $("#tradeno").val("");
                $("#area").val("");
                $("#street").val("");
                $("#community").val("");
                $("#address").val("");
                $("#username").val("");
                $("#codeid").val("");
                $("#sex").val("");
                $("#mobile").val("");
                $("#usertype").val("");
                $("#totalfee").val("");
                $("#createtime").val("");
                $("#stais").val("");
                $("#staistext").val("");
                $("#reservetime").val("");
                $("#servicelist").empty();
                return;
            }
            $(".btn-chk").click(function () {
                cleanOrderdetail();
                var orderid = $(this).data("id");
                var datastr = "cont=2&orderid=" + orderid;
                $.ajax({
                    url: "/../../controller/order.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        var result = eval("(" + e + ")");
                        $("#customer").val(result.data.customer);
                        $("#tradeno").val(result.data.tradeno);
                        $("#area").val(result.data.area);
                        $("#street").val(result.data.street);
                        $("#community").val(result.data.community);
                        $("#address").val(result.data.address);
                        $("#username").val(result.data.username);
                        $("#codeid").val(result.data.codeid);
                        $("#sex").val(result.data.sex);
                        $("#mobile").val(result.data.mobile);
                        $("#usertype").val(result.data.usertype);
                        $("#totalfee").val(result.data.totalfee);
                        $("#createtime").val(result.data.createtime);
                        $("#reservetime").val(result.data.reservetime);
                        $("#stais").val(result.data.stais);
                        $("#staistext").val(result.data.staistext);
                        $("#servicelist").html(result.data.servicelist);
                    }
                });
            });
        </script>

    </body>
</html>
